﻿using System;
using System.Collections.Generic;
using Educational.Model.Entity;
using NHibernate;
using NHibernate.Criterion;

namespace Educational.Model.EntityManager
{
    public class SchoolRoleGroupManager
    {
        public static void Add(ISession session, SchoolRoleGroup an)
        {
            session.Save(an);
        }

        public static void Delete(ISession session, SchoolRoleGroup an)
        {
            session.Delete(an);
        }

        public static void Update(ISession session, SchoolRoleGroup an)
        {
            session.Update(an);
        }

        public static SchoolRoleGroup Get(ISession session, int itemId)
        {
            return session.Get<SchoolRoleGroup>(itemId);
        }

        public static SchoolRoleGroup Get1(ISession session, int schoolId, int roleGroupId)
        {
            ICriteria c = session.CreateCriteria(typeof(SchoolRoleGroup));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
                c1.Add(Expression.Eq("schoolId", schoolId));
            ICriteria c2 = c.CreateCriteria("schoolRoles", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c.Add(Expression.Eq("roleGroupId", roleGroupId));
            return c.UniqueResult<SchoolRoleGroup>();
        }

        public static SchoolRoleGroup Get2(ISession session, int schoolId, int roleGroupId)
        {
            ICriteria c = session.CreateCriteria(typeof(SchoolRoleGroup));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c1.Add(Expression.Eq("schoolId", schoolId));
            ICriteria c2 = c.CreateCriteria("positions", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c.Add(Expression.Eq("roleGroupId", roleGroupId));
            return c.UniqueResult<SchoolRoleGroup>();
        }

        public static IList<SchoolRoleGroup> Gets1(ISession session, int schoolId)
        {
            ICriteria c = session.CreateCriteria(typeof(SchoolRoleGroup));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c1.Add(Expression.Eq("schoolId", schoolId));
            ICriteria c2 = c.CreateCriteria("schoolRoles", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c2.SetResultTransformer(NHibernate.CriteriaUtil.DistinctRootEntity);
            return c.List<SchoolRoleGroup>();
        }
    }
}
